package com.zsy.springframework.beans.factory.chouxiang;

import com.zsy.springframework.beans.factory.beanfactory.DefaultSingletonBeanRegistry;
import com.zsy.springframework.beans.factory.config.BeanDefinition;
import com.zsy.springframework.beans.factory.exception.BeansException;
import com.zsy.springframework.beans.factory.jiekou.BeanFactory;

public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String beanName) throws BeansException {
        return getGetBean(beanName, null);
    }

    @Override
    public Object getBean(String beanName, Object... args) throws BeansException {
        return getGetBean(beanName, args);
    }

    private <T> T getGetBean(String beanName, Object[] args) {
        Object bean = getSingleton(beanName);
        if (bean != null) {
            return (T) bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        return (T) createBean(beanName, beanDefinition, args);

    }

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args);


    protected abstract BeanDefinition getBeanDefinition(String beanName);
}
